import requests
import pprint

# 词形图
import jieba
import wordcloud

f = open('评论.txt', encoding='utf-8')
txt = f.read()
# print(txt)
txt_list = jieba.lcut(txt)      # 分割成一个个的词汇 返回的是一个列表
string = ' '.join(txt_list)      # 合并
print(string)

# 设置
wc = wordcloud.WordCloud(
    width=500,
    height=500,
    background_color='white',
    font_path='msyh.ttc',
    scale=15,

)
wc.generate(string)
wc.to_file('1.png')


for page in range(1, 11):
    url = 'https://club.jd.com/comment/productPageComments.action'
    param = {
        'productId': '100019679118',
        'score': '0',
        'sortType': '5',
        'page': page,
        'pageSize': '10',
        'isShadowSku' :'0',
        'fold': '1',
    }

    herders = {
        'Cookie': '__jdu=507792340; areaId=22; PCSYCityID=CN_510000_511400_0; shshshfpa=13044cd4-cdf3-6d21-6a85-15e96ad505ed-1665909242; shshshfpb=usLd9d-4oF8DsT4xpWmm-MQ; unpl=JF8EAJdnNSttXEsBDBpWGUcVTwhdW1UAH0RWPWQBXFpQSFBVSQETR0J7XlVdXhRLFh9vYBRXXVNLUg4eCysSEXteU11bD00VB2xXXAQDGhUQR09SWEBJJVhQX18NQxQDZm8HZG1bS2QFGjIfExZNVVdcWAl7JwRfVzVdXV9DUgIrAysTIAkJCFhVAEsVCiJjBFJbUEhWABoyGiIT; __jdv=122270672|haosou-search|t_262767352_haosousearch|cpc|45034921893_0_51d80e3d45d8499ebdc2596925ac29dc|1665926271754; __jdc=122270672; ipLoc-djd=22-2058-2063-41168; jsavif=0; __jda=122270672.507792340.1665909240.1665926272.1666017450.3; ip_cityCode=2058; shshshfp=7a03c9adf48e9bfdc0e3a73f1bf9f974; jwotest_product=99; 3AB9D23F7A4B3C9B=7GYTYJU35S4LKMA25QSXKTK3L6F3AV54S3AIZKU4RZOKUQWVBKCRG5MIFF53RZY5P3DXJC4NE33EQPH3RTQNCMMHCY; JSESSIONID=CB7D57FE5B626A45AD508710EB09CA6B.s1; token=35b2505c2f9c37e86c9a07fe305b2730,2,925566; __tk=14fe2ba54a66c8ba569b67bfe6b704b3,2,925566; shshshsID=aa911fcd454910240cc2b7bac5fc0971_7_1666018871973; __jdb=122270672.7.507792340|3.1666017450',
        'Referer': 'https://item.jd.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.42',
    }
    resp = requests.get(url=url, params=param, headers=herders)
    # print(resp.json()
    # pprint.pp(resp.json())
    for index in resp.json()['comments']:
        content = index['content']
        # print(content)
        with open('评论.txt', mode='a', encoding='utf-8', newline='') as f:
            f.write(content)
            f.write('\n')

